package com.example.wendao.view.ui.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.wendao.R;
import com.example.wendao.databinding.FragmentViewpagerNavTestBinding;
import com.example.wendao.view.adapter.ViewPagerTestAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ViewPagerNavTestFragment extends Fragment {


    FragmentViewpagerNavTestBinding binding;

    private ViewPager2 vp;

    private TabLayout tb;

    private ViewPagerTestAdapter viewPagerTestAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_viewpager_nav_test, container, false);

        vp = binding.viewPager;
        tb = binding.tabs;

        viewPagerTestAdapter = new ViewPagerTestAdapter(this);
        // 设置ViewPager2适配器
        vp.setAdapter(viewPagerTestAdapter);

        // 绑定 TabLayout 与 ViewPager2，第三个参数设置tab的图标、文本等参数
        new TabLayoutMediator(tb, vp,
                (tab, position) -> {
                    tab.setText(getTabTitle(position));
                }).attach();

        return binding.getRoot();
    }


    private List<String> titles = new ArrayList<>();

    {
        Collections.addAll(titles, "新闻", "论坛", "信息", "问题", "美食");
    }

    private String getTabTitle(int position) {
        return titles.get(position);
    }

}